package ar.Location.PlaceObject;

import java.util.ArrayList;

import android.util.Log;
import ar.Location.Constant.Define;
import ar.Location.PlaceApi.ApiManager;

public class PlaceCategoryManager {

	private static final String TAG = "PlaceCategoryManager";

	public static int nCategories = 0;
	public static ArrayList<PlaceCategory> listCategories = null;
	public static PlaceCategory currentPlaceCategory = null;

	public static int nDataSources = 0;
	public static ArrayList<PlaceCategory> listDataSources = null;
	public static PlaceCategory currentDataSource = null;

	public static int getCountNumOfCategories() {
		if (listCategories != null && listCategories.size() > 0) {
			return listCategories.size();
		}
		return 0;
	}

	public static int getCountNumOfDataSources() {
		if (listDataSources != null && listDataSources.size() > 0) {
			return listDataSources.size();
		}
		return 0;
	}

	public static PlaceCategory categoryAtIndex(int index) {
		if (listCategories != null) {
			if (index >= 0 && index < listCategories.size()) {
				return listCategories.get(index);
			}
		}
		return null;
	}

	public static PlaceCategory dataSourceAtIndex(int index) {
		if (listDataSources != null) {
			if (index >= 0 && index < listDataSources.size()) {
				return listDataSources.get(index);
			}
		}
		return null;
	}

	public static void initListCategories() {

		nCategories = 6;

		if (listCategories == null) {
			listCategories = new ArrayList<PlaceCategory>();
		}

		if (listCategories.size() == 0) {
			for (int i = 0; i < nCategories; i++) {

				PlaceCategory placeCategory = null;

				switch (i) {
				case Define.DF_CAFE_INDEX: {
					placeCategory = new PlaceCategory(Define.DF_CAFE_INDEX,
							Define.DF_CAFE, ApiManager.googlePlaceApiInstance());
				}
					break;
				case Define.DF_FOOD_INDEX: {
					placeCategory = new PlaceCategory(Define.DF_FOOD_INDEX,
							Define.DF_FOOD, ApiManager.googlePlaceApiInstance());
				}
					break;
				case Define.DF_PARK_INDEX: {
					placeCategory = new PlaceCategory(Define.DF_PARK_INDEX,
							Define.DF_PARK, ApiManager.googlePlaceApiInstance());
				}
					break;
				case Define.DF_BANK_INDEX: {
					placeCategory = new PlaceCategory(Define.DF_BANK_INDEX,
							Define.DF_BANK, ApiManager.googlePlaceApiInstance());
				}
					break;
				case Define.DF_UNIVERSITY_INDEX: {
					placeCategory = new PlaceCategory(
							Define.DF_UNIVERSITY_INDEX, Define.DF_UNIVERSITY,
							ApiManager.googlePlaceApiInstance());
				}
					break;
				case Define.DF_HOSPITAL_INDEX: {
					placeCategory = new PlaceCategory(Define.DF_HOSPITAL_INDEX,
							Define.DF_HOSPITAL,
							ApiManager.googlePlaceApiInstance());
				}
					break;
				}

				if (placeCategory != null) {
					listCategories.add(placeCategory);
				}
			}
		}
	}

	public static synchronized void loadListCategories(float lat, float lng,
			int radiusMetters) {

		Log.d(TAG, "Goi ham load list categories");
		
/*		String latitude = String.valueOf(lat);
		String longitude = String.valueOf(lng);

		clearListPlaceCategories();

		nCategories = listCategories.size();

		synchronized (listCategories) {
			for (int i = 0; i < nCategories; i++) {

				PlaceCategory placeCategory = listCategories.get(i);

				synchronized (placeCategory) {
					if (placeCategory != null) {
						switch (i) {
						case Define.DF_CAFE_INDEX: {
							placeCategory.loadListPlaces(latitude, longitude,
									"cafe", radiusMetters);
						}
							break;
						case Define.DF_FOOD_INDEX: {
							placeCategory.loadListPlaces(latitude, longitude,
									"food", radiusMetters);
						}
							break;
						case Define.DF_PARK_INDEX: {
							placeCategory.loadListPlaces(latitude, longitude,
									"park", radiusMetters);
						}
							break;
						case Define.DF_BANK_INDEX: {
							placeCategory.loadListPlaces(latitude, longitude,
									"bank", radiusMetters);
						}
							break;
						case Define.DF_UNIVERSITY_INDEX: {
							placeCategory.loadListPlaces(latitude, longitude,
									"university", radiusMetters);
						}
							break;
						case Define.DF_HOSPITAL_INDEX: {
							placeCategory.loadListPlaces(latitude, longitude,
									"hospital", radiusMetters);
						}
							break;
						}
					}
				}
			}
		}*/
	}

	public static void initListDataSources() {

		nDataSources = 5;

		if (listDataSources == null) {
			listDataSources = new ArrayList<PlaceCategory>();
		}

		if (listDataSources.size() == 0) {
			for (int i = 0; i < nDataSources; i++) {

				PlaceCategory placeCategory = null;

				switch (i) {
				case Define.DS_GOOGLE_PLACE_INDEX: {
					placeCategory = new PlaceCategory(
							Define.DS_GOOGLE_PLACE_INDEX,
							Define.DS_GOOGLE_PLACE,
							ApiManager.googlePlaceApiInstance());
				}
					break;
				case Define.DS_WIKIPEDIA_INDEX: {
					placeCategory = new PlaceCategory(
							Define.DS_WIKIPEDIA_INDEX, Define.DS_WIKIPEDIA,
							ApiManager.wikipediaApiInstance());
				}
					break;
				case Define.DS_FLICKR_INDEX: {
					placeCategory = new PlaceCategory(Define.DS_FLICKR_INDEX,
							Define.DS_FLICKR, ApiManager.flickrApiInstance());
				}
					break;
				case Define.DS_FOURSQUARE_INDEX: {
					placeCategory = new PlaceCategory(
							Define.DS_FOURSQUARE_INDEX, Define.DS_FOURSQUARE,
							ApiManager.foursquareApiInstance());
				}
					break;
				case Define.DS_EVENTFUL_INDEX: {
					placeCategory = new PlaceCategory(Define.DS_EVENTFUL_INDEX,
							Define.DS_EVENTFUL,
							ApiManager.eventfulApiInstance());
				}
					break;
				}

				if (placeCategory != null) {
					listDataSources.add(placeCategory);
				}
			}
		}
	}

	public static synchronized void loadListDataSources(float lat, float lng,
			int radiusMetters) {

		Log.d(TAG, "Goi ham load list data source");
		
/*		String latitude = String.valueOf(lat);
		String longitude = String.valueOf(lng);

		clearListDataSources();

		nDataSources = listDataSources.size();

		synchronized (listDataSources) {
			for (int i = 0; i < nDataSources; i++) {

				PlaceCategory dataSource = listDataSources.get(i);
				synchronized (dataSource) {
					if (dataSource != null) {
						dataSource.loadListPlaces(latitude, longitude,
								radiusMetters);
					}
				}
			}
		}*/
	}

	private static void clearListDataSources() {
		// TODO Auto-generated method stub
		if (listDataSources == null) {
			initListDataSources();
		} else {
			if (listDataSources.size() > 0) {
				int count = listDataSources.size();
				for (int i = 0; i < count; i++) {
					if (listDataSources.get(i) != null) {
						listDataSources.get(i).finishLoading = false;
						listDataSources.get(i).clear();
					}
				}
			}
		}
	}

	public static void clearListPlaceCategories() {
		// TODO Auto-generated method stub
		if (listCategories == null) {
			initListCategories();
		} else {
			if (listCategories.size() > 0) {
				int count = listCategories.size();
				for (int i = 0; i < count; i++) {
					if (listCategories.get(i) != null) {
						listCategories.get(i).finishLoading = false;
						listCategories.get(i).clear();
					}
				}
			}
		}
	}

	public static boolean isProcessingLoadListCategories() {
		if (listCategories == null) {
			return false;
		} else {
			if (listCategories.size() > 0) {
				int count = listCategories.size();
				for (int i = 0; i < count; i++) {
					if (listCategories.get(i) != null) {
						if (listCategories.get(i).isProcessingLoadByType()) {
							return true;
						}
					}
				}
				return false;
			}
			return false;
		}
	}

	public static boolean isProcessingLoadListDataSources() {
		if (listDataSources == null) {
			return false;
		} else {
			if (listDataSources.size() > 0) {
				int count = listDataSources.size();
				for (int i = 0; i < count; i++) {
					if (listDataSources.get(i) != null) {
						if (listDataSources.get(i).isProcessingLoadByPoint()) {
							return true;
						}
					}
				}
				return false;
			}
			return false;
		}
	}
}
